package gui;

import gameObjects.Castle;
import gameObjects.Unit;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import castlewarserver.Connector;

public class MainWindow {

	JFrame mainFrame = new JFrame();
	MainPanel  layeredPane;
	JScrollPane scrollPane;
	JButton endTurnButton;

	GuiObject clickedObject = null;
	MouseEvents mouseEventListener;

	public ArrayList<GuiObject> units = new ArrayList<GuiObject>();
	public GuiObject castleOrange;
	public GuiObject castleBlue;	
	public String actualPlayer="ORANGE";


	public String player;
	public String endpoint = "http://localhost:8080/CastleWarServer/services/Game";
	private JLayeredPane menuLayeredPane;
	private JLabel lblYourGold;
	private JLabel lblCastleHp;
	JLabel goldLabel;
	JLabel castleHpLabel;
	private JLabel lblNewLabel;
	private JLabel label_1;
	private JLabel armsLabel;



	public static void main(String[] args) {

		// ustawianie L&F
		try
		{

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		}
		catch(ClassNotFoundException e1)
		{
			e1.printStackTrace();
		}
		catch(InstantiationException e1)
		{
			e1.printStackTrace();
		}
		catch(IllegalAccessException e1)
		{
			e1.printStackTrace();
		}
		catch(UnsupportedLookAndFeelException e1)
		{
			e1.printStackTrace();
		}

		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					MainWindow mainWindow = new MainWindow("ORANGE","http://localhost:8080/CastleWarServer/services/Game");
				}

				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
		});

	}
	public MainWindow(String player, String endpoint){
		this.player=player;
		this.endpoint=endpoint;
		init();

		MapLoader mapLoader= new MapLoader(this);
		mapLoader.loadMap("mapa0.json", "UTF-8");

		mouseEventListener.initGame();
		mainFrame.getContentPane().setMaximumSize(new Dimension(960, 960));
		mainFrame.setTitle("Castle War");

		mainFrame.setLocationRelativeTo(null);
		mainFrame.setVisible(true);

	}
	private void init(){
		UIManager.put("ScrollBar.thumb", new Color (248,218,88));
		mouseEventListener = new MouseEvents(this,new Connector(endpoint,this));

		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setBounds(10, 10, 550, 600);
		mainFrame.setResizable(false);
		layeredPane= new MainPanel();


		layeredPane.setPreferredSize(new Dimension(200,300));
		scrollPane = new JScrollPane(layeredPane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setBorder(null);

		scrollPane.setLayout(new ScrollPaneLayout());

		scrollPane.getVerticalScrollBar().setUI( new CustomScrollBarUI());
		scrollPane.getHorizontalScrollBar().setUI( new CustomScrollBarUI());
		scrollPane.setBackground(new Color (248,218,88));

		mainFrame.getContentPane().add(scrollPane);


		menuLayeredPane = new JLayeredPane();
		menuLayeredPane.setPreferredSize(new Dimension(1000, 65));
		mainFrame.getContentPane().add(menuLayeredPane, BorderLayout.SOUTH);

		endTurnButton = new JButton(new ImageIcon ("images\\buttonEndTurn.png"));
		endTurnButton.setBounds(217, 5, 100, 50);
		endTurnButton.setPreferredSize( new Dimension(100,50));
		endTurnButton.setOpaque(false);
		endTurnButton.setMinimumSize(new Dimension(100,50));
		endTurnButton.setMaximumSize(new Dimension(100,50));
		endTurnButton.addActionListener(mouseEventListener);
		menuLayeredPane.setLayout(null);
		menuLayeredPane.add(endTurnButton);

		lblYourGold = new JLabel("Your gold:");
		lblYourGold.setForeground(new Color(255, 215, 0));
		lblYourGold.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblYourGold.setBounds(414, 11, 67, 14);
		menuLayeredPane.add(lblYourGold);

		lblCastleHp = new JLabel("Castle HP:");
		lblCastleHp.setForeground(new Color(255, 215, 0));
		lblCastleHp.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblCastleHp.setBounds(414, 35, 67, 14);
		menuLayeredPane.add(lblCastleHp);

		goldLabel = new JLabel("0");
		goldLabel.setForeground(new Color(255, 215, 0));
		goldLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
		goldLabel.setBounds(488, 11, 46, 14);
		menuLayeredPane.add(goldLabel);

		castleHpLabel = new JLabel("0");
		castleHpLabel.setForeground(new Color(255, 215, 0));
		castleHpLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
		castleHpLabel.setBounds(488, 36, 46, 14);
		menuLayeredPane.add(castleHpLabel);

		lblNewLabel = new JLabel(new ImageIcon ("images\\gold.png"));
		lblNewLabel.setOpaque(false);
		lblNewLabel.setBounds(518, 11, 16, 16);
		menuLayeredPane.add(lblNewLabel);

		label_1 = new JLabel(new ImageIcon ("images\\health.png"));
		label_1.setOpaque(false);
		label_1.setBounds(518, 35, 16, 16);
		menuLayeredPane.add(label_1);


		for (int w = 0 ; w< mainFrame.getWidth(); w+=150 ){
			for (int h =0; h < 60; h+=150){
				JLabel label = new JLabel(new ImageIcon("images\\menuBackground.png"));
				label.setBounds(w, h, 150, 150);
				menuLayeredPane.add(label,-10);
			}
		}


		layeredPane.addMouseListener(mouseEventListener);
		mouseEventListener.connector.loadCastles();
		mouseEventListener.connector.loadAllUnits();

		Castle castle;
		if (player.equalsIgnoreCase("ORANGE")){
			castle = (Castle)castleOrange.getGameObject();
			armsLabel = new JLabel(new ImageIcon("images\\armsOrange.png"));
		}
		else{
			castle = (Castle)castleBlue.getGameObject();
			armsLabel = new JLabel(new ImageIcon("images\\armsBlue.png"));
		}
		mouseEventListener.castleMenu= new CastleMenu(mouseEventListener.connector,player,this,castle);
		goldLabel.setText(String.valueOf(castle.getGold()));
		castleHpLabel.setText(String.valueOf(castle.getHp()));


		armsLabel.setBounds(10, 2, 60, 60);
		menuLayeredPane.add(armsLabel);
		menuLayeredPane.setLayer(armsLabel, 50);
		//Dodanie GuiObjects do sceny
		drawAllGuiObjects();

		initGuiObject(castleOrange);
		initGuiObject(castleBlue);

	}

	public void drawAllGuiObjects(){
		for(GuiObject obj:units){
			initGuiObject(obj);
		}

	}

	private void initGuiObject(GuiObject obj){
		obj.addMouseListener(mouseEventListener);
		int x= obj.getGameObject().getX()*GuiObject.W_SIZE;
		int y=obj.getGameObject().getY()*GuiObject.H_SIZE;
		obj.setBounds(x, y, GuiObject.W_SIZE, GuiObject.H_SIZE);

		layeredPane.add(obj);
		int layer = 50;
		if (obj.getGameObject().getPlayer().equalsIgnoreCase(player)){
			if (obj.getGameObject().getObjectType().equalsIgnoreCase("Castle")){
				layer = 51;
			}
			else{
				layer = 52;
			}
		}
		else{
			if (obj.getGameObject().getObjectType().equalsIgnoreCase("Castle")){
				layer = 49;
			}
			else{
				layer = 48;
			}
		}

		layeredPane.setLayer(obj, layer);
	}

	public void addUnit(Unit unit){
		GuiObject obj = new GuiObject(unit);
		initGuiObject(obj);
		units.add(obj);
	}

	public void reDrawGuiObject(GuiObject obj){
		int x= obj.getGameObject().getX()*GuiObject.W_SIZE;
		int y=obj.getGameObject().getY()*GuiObject.H_SIZE;
		obj.setBounds(x, y, GuiObject.W_SIZE, GuiObject.H_SIZE);

		obj.repaint();
	}

	public void endOfGame(){
		JOptionPane.showMessageDialog(mainFrame, "Game Over\n Player "+actualPlayer+" wins.");
		actualPlayer = "NONE";
		endTurnButton.setEnabled(false);
	}
}
